#ifdef __ANDROID__

#include "AApp.h"
#include "ARenderer.h"
#include "AAssetMgr.h"
#include "AVersion.h"
#include "AUtil.h"

JavaVM* AApp::s_javaVM = nullptr;

/**
 * @brief Entry point of the Android NDK application
 * @note normally be called before Java Entry Activity's onCreate
 */
extern "C" JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM* jvm, void* reserved) {
	AApp::setJavaVM(jvm);
	// check if JNI version is compatible
	int CheckJNIVersion = JNI_VERSION_1_6;
	JNIEnv* env;
	if (jvm->GetEnv(reinterpret_cast<void**>(&env), CheckJNIVersion) != JNI_OK)
		return JNI_ERR;

	__android_log_print(ANDROID_LOG_DEBUG, "JNI_OnLoad", "JNI_OnLoad called, dynamic lib load success!");

	AVersion::printInfo();

	return CheckJNIVersion;
}

/**
 * @brief init Java AssetManager to NDK (AAssetManager*)
 * internalPath: /data/user/0/com.example.sq3dndk/files
 * externalPath: /storage/emulated/0/Android/data/com.example.sq3dndk/files
 */
extern "C" JNIEXPORT jobjectArray JNICALL
Java_com_hb_sq3d_Viewer3D_onCreate(JNIEnv* env, jclass /* jclz */, jobject assetManager, jstring internalPath, jstring externalPath) {
	const char *internalCPath = env->GetStringUTFChars(internalPath, nullptr);
	const char *externalCPath = env->GetStringUTFChars(externalPath, nullptr);
	
	AAssetMgr::get()->init(env, assetManager, internalCPath, externalCPath);

	env->ReleaseStringUTFChars(internalPath, internalCPath);
	env->ReleaseStringUTFChars(externalPath, externalCPath);

	AApp::init();

	CVector<std::string> jArr;
	std::string projStr = "";
	if (Proj::isSQ()) projStr = "IsProject_SQ";
	else if (Proj::isSSQK()) projStr = "IsProject_SSQK";
	jArr << projStr << std::to_string(Config::Released) << Config::View3D_BgLightImgPath << Config::View3D_BgDarkImgPath << Config::View3D_ZhichePngPath;
	return AUtil::createJStringArray(env, jArr);
}

extern "C" JNIEXPORT jintArray JNICALL
Java_com_hb_sq3d_Viewer3D_getSize(JNIEnv* env, jclass /* jclz */) {
	CVector<int> jArr;
	jArr << W3D << H3D;
	return AUtil::createJIntArray(env, jArr);
}
#endif